package com.cangsan.erhai.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.SysRole;
import com.cangsan.erhai.entity.SysRoleMenu;
import com.cangsan.erhai.service.SysRoleMenuService;
import com.cangsan.erhai.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("role_name",pageBean.getQuery());

        Page<SysRole> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<SysRole> pageResult = sysRoleService.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        result.put("userList",pageResult.getRecords());
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }
    @PostMapping("/saveOrUpdate")

    public R add(@RequestBody SysRole sysRole){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_key",sysRole.getRoleKey());
        List<SysRole> list = sysRoleService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            return R.fail("角色编码重复");
        }

        sysRoleService.saveOrUpdate(sysRole);
        return R.ok();
    }

    @GetMapping("/getMenu/{roleId}")

    public R getMenu(@PathVariable("roleId") Long roleId){
        return R.ok( sysRoleMenuService.getMenuByRoleId(roleId));
    }


    @PostMapping("/addMenu")
    public R addMenu(@RequestBody JSONObject object){
        Long roleId = object.getLong("roleId");
        JSONArray roleMenu = object.getJSONArray("menu");
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        for (int i = 0; i < roleMenu.size(); i++) {
            JSONObject jsonObject = roleMenu.getJSONObject(i);
            Long menuId = jsonObject.getLong("menuId");
            SysRoleMenu sysRoleMenu =new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenus.add(sysRoleMenu);
        }
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<SysRoleMenu>();
        LambdaQueryWrapper<SysRoleMenu> eq = wrapper.eq(SysRoleMenu::getRoleId, roleId);
        sysRoleMenuService.remove(eq);
        sysRoleMenuService.saveBatch(sysRoleMenus);
        return R.ok();
    }


    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        sysRoleService.removeById(id);
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<SysRoleMenu>();
        LambdaQueryWrapper<SysRoleMenu> eq = wrapper.eq(SysRoleMenu::getRoleId, id);
        sysRoleMenuService.remove(eq);

        return R.ok("删除成功");
    }

}
